<?php
namespace Admin\Controller;

use Think\Controller;

class LogsController extends CommenController
{
    /**
     * 用户登录日志
     */
    public function user_login_log()
    {
	    $info = M('user_login_log', '', 'USER_R');
	    $user_db=M('user','','USER_R');
        $where=MycommonController::set_where('','','login_time');
        if (!empty(I('content'))) {
            $content = I('content');
            $vid = $user_db->WHERE("truename='$content' or mobile='$content'")->getField('vid');
            if (!empty($vid)) {
                $where['vid'] = $vid;
            } else {
                $where['address'] = array('like',$content.'%');
            }
        }
        unset($where['_string']);
//        $redis=new \Redis();
//        $redis->pconnect(C('REDIS_HOST'), 6379);
//        $redis->auth(C('REDIS_PWD'));

        $tol=$info->WHERE($where)->SELECT(false);
        if(!empty(self::$redis->get('user_login_log'.$tol))){
            $tol=self::$redis->get('user_login_log'.$tol);
        }else{
            $count = $info->WHERE($where)->COUNT();
            self::$redis->set('user_login_log'.$tol,$count);
            self::$redis->expire('user_login_log'.$tol,24*3600);
            $tol=$count;
        }
        $row = '10';
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        $list = $info->WHERE($where)->FIELD("id")->ORDER("id desc")->LIMIT($page->listfirst,$page->listRows)->SELECT();
        $list=array_column($list,'id');
        $list=$info->WHERE(['id'=>['in',$list]])->ORDER("id desc")->LIMIT($row)->SELECT();

        foreach ($list as $k => $v) {
            $list[$k]['login_time'] = date('Y-m-d H:i:s', $v['login_time']);
            $user=$user_db->WHERE(['vid'=>$v['vid']])->FIELD("truename,mobile")->FIND();
            $list[$k]['truename']=$user['truename'];
            $list[$k]['mobile']=substr($user['mobile'],0,3).'*****'.substr($user['mobile'],-3,3);
//            $list[$k]['mobile']=$user['mobile'];
        }

        $this->assign(array(
            'list' => $list,
            'fpage' => $fpage,
            'tol' => $tol
        ));
        $this->display();
    }

    /**
     * 管理员操作日志
     */
    public function admin_log()
    {
        $info = M('admin_log', '', 'USER_R');
        $where=MycommonController::set_where('',array('a.phone','a.name'),'al.ctime',array('al.content'),true);
//        var_dump($where);
        $tol = $info->JOIN("al LEFT JOIN admin a ON al.aid=a.id")->WHERE($where)->COUNT();
        $row = '20';
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        $list = $info->JOIN("al LEFT JOIN admin a ON al.aid=a.id")->WHERE($where)->FIELD("al.id")->LIMIT($page->listfirst, $page->listRows)->ORDER("al.id desc")->SELECT();
        $list=array_column($list,'id');
        $list=$info->JOIN("al LEFT JOIN admin a ON al.aid=a.id")->WHERE(['al.id'=>['in',$list]])->FIELD("al.*,a.name,a.phone")->ORDER("al.id desc")->SELECT();
        foreach ($list as $k => $v) {
            $list[$k]['ctime'] = date('Y-m-d H:i:s', $v['ctime']);
        }
        $this->assign(array(
            'list' => $list,
            'fpage' => $fpage,
            'tol' => $tol
        ));
        $this->display();
    }

    /**
     * 额度修改日志
     */
    public function quota_log(){
        $info=M("user_quota_record",'','CAIWU_R');
        $where=MycommonController::set_where(array('uq.type'),array('u.mobile','u.truename','uq.admin_name'),'uq.ctime');
//        var_dump($where);
        $tol = $info->JOIN("uq LEFT JOIN dxd_user.user u ON uq.vid=u.vid")->WHERE($where)->COUNT();
        $row = '20';
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        $list = $info->JOIN("uq LEFT JOIN dxd_user.user u ON uq.vid=u.vid")->WHERE($where)->FIELD("uq.id")->LIMIT($page->listfirst, $page->listRows)->ORDER("uq.id desc")->SELECT();
        $list=array_column($list,'id');
        $list=$info->JOIN("uq LEFT JOIN dxd_user.user u ON uq.vid=u.vid")->WHERE(['uq.id'=>['in',$list]])->FIELD("uq.*,u.mobile,u.truename")->ORDER("uq.id desc")->SELECT();
//        var_dump($list);
        foreach ($list as $k => $v) {
            $list[$k]['ctime'] = date('Y-m-d H:i:s', $v['ctime']);
            if(strpos($v['money'],'-')===false){
                $list[$k]['money']='+'.$v['money'];
            }
        }
        $this->assign(array(
            'list' => $list,
            'fpage' => $fpage,
            'tol' => $tol
        ));
        $this->display();
    }

}